﻿package com.snakedickgames.common.tweenmax
{
    import com.pblabs.engine.debug.Logger;
    import com.pblabs.engine.PBE;
    import com.pblabs.engine.entity.EntityComponent;
    import com.pblabs.animation.AnimationEvent;
    import flash.events.Event;
    import com.greensock.events.TweenEvent;
    
    public class DestroyWhenComplete extends EntityComponent
    {
        override protected function onAdd():void 
        {
            super.onAdd();
            owner.eventDispatcher.addEventListener(TweenEvent.COMPLETE, prepareToDestroy);
        }
        
        override protected function onRemove():void 
        {
            owner.eventDispatcher.removeEventListener(TweenEvent.COMPLETE, prepareToDestroy);
            super.onRemove();
        }
        
        protected function prepareToDestroy(event:Event):void 
        {
            PBE.callLater(_destroyMe);
        }
        
        protected function _destroyMe():void
        {
            if (owner)
                owner.destroy();
        }
    }
}